/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.panel.person.edit;

import hu.glassy.core.model.Person;
import hu.glassy.core.service.PersonService;
import hu.glassy.web.component.common.CustomAjaxSubmitLink;
import hu.glassy.web.component.feedback.CustomFeedbackPanel;
import hu.glassy.web.component.visitor.ErrorMessageFormVisitor;
import hu.glassy.web.panel.common.AdminPanel;
import java.util.Date;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.DateValidator;
import org.apache.wicket.validation.validator.StringValidator;

/**
 * Allows the user to modify the person's base data.
 *
 * @author Alexander Molnár <molnar.alexander@sonrisa.hu>
 */
public final class PersonDataEditPanel extends AdminPanel<Person>
{

  @SpringBean
  PersonService personService;

  /**
   * Simple Constructor
   *
   * @param id Component ID
   */
  public PersonDataEditPanel(String id)
  {
    this(id, Model.of(new Person()));
  }

  /**
   * Constructor with model
   *
   * @param id    Component ID
   * @param model The model to use
   */
  public PersonDataEditPanel(String id, IModel<Person> model)
  {
    super(id, model);
    setDefaultModel(new CompoundPropertyModel<Person>(model));

    // Form
    Form<Person> form = new StatelessForm<Person>("personEditForm", model)
    {

      @Override
      protected void onSubmit()
      {
        super.onSubmit();
        personService.saveOrUpdate(PersonDataEditPanel.this.getMainObject());
      }

    };

    // Feedback Panel
    form.add(new CustomFeedbackPanel("feedbackPanel", form));

    // Name field
    TextField<String> nameInput = new TextField<String>("name");
    nameInput.setRequired(Person.NAME_REQUIRED);
    nameInput.add(StringValidator.maximumLength(Person.NAME_LENGTH));
    form.add(nameInput);

    // Display Name field
    TextField<String> displayNameInput = new TextField<String>("displayName");
    displayNameInput.setRequired(Person.DISPLAYNAME_REQUIRED);
    displayNameInput.add(StringValidator.maximumLength(Person.DISPLAYNAME_LENGTH));
    form.add(displayNameInput);

    // Short description
    TextArea<String> shortDescriptionInput = new TextArea<String>("shortDescription");
    shortDescriptionInput.setRequired(Person.SHORT_DESCRIPTION_REQUIRED);
    shortDescriptionInput.add(StringValidator.maximumLength(Person.SHORT_DESCRIPTION_LENGTH));
    form.add(shortDescriptionInput);

    // Long description
    TextArea<String> longDescriptionInput = new TextArea<String>("longDescription");
    longDescriptionInput.setRequired(Person.LONG_DESCRIPTION_REQUIRED);
    longDescriptionInput.add(StringValidator.maximumLength(Person.LONG_DESCRIPTION_LENGTH));
    form.add(longDescriptionInput);

    // Birthdate input
    DateTextField birthDateInput = new DateTextField("birthDate", new StyleDateConverter(true));
    birthDateInput.setRequired(Person.BIRTH_DATE_REQUIRED);
    birthDateInput.add(DateValidator.maximum(new Date()));
    form.add(birthDateInput);

    // Date picker for Date input
    DatePicker datePicker = new DatePicker();
    datePicker.setShowOnFieldClick(true);
    datePicker.setAutoHide(true);
    birthDateInput.add(datePicker);

    // Birthplace input
    TextField<String> birthPlaceInput = new TextField<String>("birthPlace");
    birthPlaceInput.setRequired(Person.BIRTH_PLACE_REQUIRED);
    birthPlaceInput.add(StringValidator.maximumLength(Person.BIRTH_PLACE_LENGTH));
    form.add(birthPlaceInput);

    // Submit button
    AjaxSubmitLink submitLink = new CustomAjaxSubmitLink("submitButton");
    form.add(submitLink);

    form.visitChildren(new ErrorMessageFormVisitor<String>());
    add(form);
  }
}
